<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="id">
        <el-input v-model="queryParams.id" placeholder="请输入编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="法人代表" prop="legalName">
        <el-input v-model="queryParams.legalName" placeholder="请输入法人代表" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="注册地址" prop="address">
        <el-input v-model="queryParams.address" placeholder="请输入注册地址" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="专属客服" prop="accountAnager">
        <el-input v-model="queryParams.accountAnager" placeholder="请输入专属客服" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="认证状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择认证状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.AGRI_AUDIT_STATUS)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="品牌供应链" prop="supply">
        <el-select v-model="queryParams.supply" placeholder="请选择品牌供应链" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_BOOLEAN_STRING)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="企业全称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入企业全称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="经营类型" prop="bizType">
        <el-select v-model="queryParams.bizType" placeholder="请选择经营类型" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.AGRI_COMPANY_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态 " prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态 " clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="企业类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择企业类型" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.AGRI_COMPANY_CATEGORY)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="直属区域id" prop="areaId">
        <el-input v-model="queryParams.areaId" placeholder="请输入直属区域id" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="联系人" prop="linkName">
        <el-input v-model="queryParams.linkName" placeholder="请输入联系人" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="联系电话" prop="linkTel">
        <el-input v-model="queryParams.linkTel" placeholder="请输入联系电话" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="信用代码" prop="legalCode">
        <el-input v-model="queryParams.legalCode" placeholder="请输入信用代码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['agri:company:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['agri:company:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="法人代表" align="center" prop="legalName" />
      <el-table-column label="营业执照" align="center" prop="legalPic" />
      <el-table-column label="成立时间" align="center" prop="bornDay" />
      <el-table-column label="注册地址" align="center" prop="address" />
      <el-table-column label="主营业务" align="center" prop="business" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="专属客服" align="center" prop="accountAnager" />
      <el-table-column label="经度" align="center" prop="lng" />
      <el-table-column label="纬度" align="center" prop="lat" />
      <el-table-column label="认证" align="center" prop="auditStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.AGRI_AUDIT_STATUS" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>
      <el-table-column label="认证编号" align="center" prop="auditCode" />
      <el-table-column label="认证时间" align="center" prop="auditTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.auditTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="品牌供应链" align="center" prop="supply" width="100">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.supply" />
        </template>
      </el-table-column>
      <el-table-column label="企业全称" align="center" prop="name" />
      <el-table-column label="经营类型" align="center" prop="bizType">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.AGRI_COMPANY_TYPE" :value="scope.row.bizType" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="企业类型" align="center" prop="type">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.AGRI_COMPANY_CATEGORY" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="直属区域" align="center" prop="areaId" />
      <el-table-column label="联系人" align="center" prop="linkName" />
      <el-table-column label="联系电话" align="center" prop="linkTel" />
      <el-table-column label="信用代码" align="center" prop="legalCode" />
      <el-table-column label="基地介绍" align="center" prop="farmInfo" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['agri:company:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['agri:company:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="法人代表" prop="legalName">
          <el-input v-model="form.legalName" placeholder="请输入法人代表" />
        </el-form-item>
        <el-form-item label="法人身份证" prop="legalIdcard">
          <el-input v-model="form.legalIdcard" placeholder="请输入法人身份证" />
        </el-form-item>
        <el-form-item label="营业执照">
          <imageUpload v-model="form.legalPic"/>
        </el-form-item>
        <el-form-item label="注册资本" prop="capital">
          <el-input v-model="form.capital" placeholder="请输入注册资本" />
        </el-form-item>
        <el-form-item label="成立时间" prop="bornDay">
          <el-date-picker clearable v-model="form.bornDay" type="date" value-format="timestamp" placeholder="选择成立时间" />
        </el-form-item>
        <el-form-item label="注册地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入注册地址" />
        </el-form-item>
        <el-form-item label="主营业务" prop="business">
          <el-input v-model="form.business" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="专属客服" prop="accountAnager">
          <el-input v-model="form.accountAnager" placeholder="请输入专属客服" />
        </el-form-item>
        <el-form-item label="经度" prop="lng">
          <el-input v-model="form.lng" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="认证：" v-if="form.auditStatus==`1`" prop="auditStatus">
          <el-select v-model="form.auditStatus" placeholder="请选择认证">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.AGRI_AUDIT_STATUS)"
                       :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="品牌供应链" prop="supply">
          <el-radio-group v-model="form.supply">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_BOOLEAN_STRING)"
                      :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="企业全称" prop="name">
          <el-input v-model="form.name" placeholder="请输入企业全称" />
        </el-form-item>
        <el-form-item label="经营类型" prop="bizType">
          <el-select v-model="form.bizType" placeholder="请选择经营类型">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.AGRI_COMPANY_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
                      :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="企业类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择企业类型">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.AGRI_COMPANY_CATEGORY)"
                       :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="直属区域id" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入直属区域id" />
        </el-form-item>
        <el-form-item label="联系人" prop="linkName">
          <el-input v-model="form.linkName" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="linkTel">
          <el-input v-model="form.linkTel" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="信用代码" prop="legalCode">
          <el-input v-model="form.legalCode" placeholder="请输入信用代码" />
        </el-form-item>
        <el-form-item label="基地介绍" prop="farmInfo">
          <el-input v-model="form.farmInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createCompany, updateCompany, deleteCompany, getCompany, getCompanyPage, exportCompanyExcel } from "@/api/agri/company";
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "Companies",
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业信息列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        id: null,
        legalName: null,
        address: null,
        accountAnager: null,
        auditStatus: null,
        supply: null,
        name: null,
        bizType: null,
        status: null,
        type: null,
        areaId: null,
        linkName: null,
        linkTel: null,
        legalCode: null,
        farmInfo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getCompanyPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        legalName: undefined,
        legalIdcard: undefined,
        legalPic: undefined,
        capital: undefined,
        bornDay: undefined,
        address: undefined,
        business: undefined,
        remark: undefined,
        accountAnager: undefined,
        lng: undefined,
        lat: undefined,
        auditStatus: undefined,
        supply: undefined,
        name: undefined,
        bizType: undefined,
        status: undefined,
        type: undefined,
        areaId: undefined,
        linkName: undefined,
        linkTel: undefined,
        legalCode: undefined,
        farmInfo: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getCompany(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateCompany(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createCompany(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除企业信息编号为"' + id + '"的数据项?').then(function() {
          return deleteCompany(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有企业信息数据项?').then(() => {
          this.exportLoading = true;
          return exportCompanyExcel(params);
        }).then(response => {
          this.$download.excel(response, '企业信息.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
